package com.zg.autoform.web;

import com.zg.autoform.service.FormDivService;
import com.zg.autoform.vo.FormDivVo;
import com.zg.autoform.web.req.FormDivAddReq;
import com.zg.autoform.web.req.FormDivEditReq;
import com.zg.autoform.web.req.FormDivGetReq;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoform.DiyFormDiv;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * DESCRIPTION
 *
 * @author zhanfeng.liang
 * @since 2025-07-17
 */
@RestController
@RequiredArgsConstructor
@Api(tags = {"表单个性化管理"})
@RequestMapping("/form/div")
public class FormDivController {

    private final FormDivService formDivService;
    @PostMapping("/add")
    public R add(@RequestBody @Valid FormDivAddReq formAddDivReq) {
        formDivService.add(formAddDivReq);
        return R.success();
    }


    @PostMapping("/edit")
    public R edit( @RequestBody @Valid  FormDivEditReq editReq) {
        formDivService.edit(editReq);
        return R.success();
    }

    @PostMapping("/remove")
    public R remove(@RequestBody  List<Long> ids) {
        formDivService.remove(ids);
        return R.success();
    }

    @PostMapping("/get")
    public R<Object> get( @RequestBody @Valid  FormDivGetReq editReq) {
        DiyFormDiv div=formDivService.getByFormAndUserId(editReq.getFormId(),editReq.getUserId());
        if(div!=null){
            FormDivVo divFormVo=new FormDivVo();
            divFormVo.setFormId(div.getFormId());
            divFormVo.setUserId(div.getUserId());
            divFormVo.setContent(new String[]{div.getContent()});
            return R.success(divFormVo);
        }

        return R.success(new String[]{});
    }
}
